package com.example.springboot08.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot08.entity.Student;
import com.example.springboot08.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    //查询所有学生
    @RequestMapping("/findAll")
    public List<Student> findAllStudent() {
        return studentService.list(null);
    }

    //通过id查询学生
    @RequestMapping("/findById")
    public Student findStudentById(@RequestParam Long id) {
        return studentService.getById(id);
    }

    //添加学生信息
    @RequestMapping("/save")
    public boolean saveStudent(@RequestBody Student student) {
        return studentService.save(student);
    }

    //修改学生信息
    @RequestMapping("/update")
    public boolean updateStudentById(@RequestBody Student student) {
        return studentService.updateById(student);
    }

    //根据id删除学生信息
    @RequestMapping("/removeById")
    public boolean removeStudentById(@RequestParam Long id) {
        return studentService.removeById(id);
    }

    //批量删除学生信息
    @RequestMapping("/removeByIds")
    public boolean removeStudentByIds(@RequestBody List<Integer> ids) {
        return studentService.removeByIds(ids);
    }

    //模糊查询学生信息
    @RequestMapping("mohuchaxun")
    public List<Student> mohuchaxun(@RequestParam String name){
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.like("sname",name);
        List<Student> list = studentService.list(wrapper);
        return list;
    }
    //分页查询学生信息
    @RequestMapping("testPage")
    public Page<Student> testPage(){
        Page<Student> page = new Page<>(2,2);
        studentService.page(page,null);
        page.getRecords().forEach(System.out::println);
        System.out.println("总页数==>"+page.getTotal());
        return page;
    }





}

